package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.movecar.R;
import com.movecar.model.CloudBeansBean;
import com.movecar.model.GroupBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.PopupWindowAnimation;
import com.movecar.utils.RequestResultCallback;
import com.movecar.widget.CustomProgressDialog;

/**
 * 云豆提现 银行卡提现
 * 
 * @author Zhao.JQ
 * 
 */

public class CloudCoinsBankRechargeActivity extends Activity implements android.view.View.OnClickListener {
	private EditText cardNumber, card_again, name;
	private InputMethodManager imm;
	private Button ok, back;
	private CustomProgressDialog progressDialog = null;
	private EditText cash_edit;
	private CloudBeansBean mCloudBeansBean;
	private String bankId;
	private View mainView;
	private LayoutInflater layoutInflater;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);mainView = layoutInflater.inflate(R.layout.bank_recharge, null);
		setContentView(mainView);	
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		bankId = getIntent().getStringExtra("bankId");
		initView();
	}

	private void initView() {

		back = (Button) findViewById(R.id.btn_left);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		cardNumber = (EditText) findViewById(R.id.cardNumber);
		card_again = (EditText) findViewById(R.id.card_again);
		name = (EditText) findViewById(R.id.name);
		cash_edit = (EditText) findViewById(R.id.cash_edit);
		ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(this);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("银行提现");
		imm.showSoftInput(cardNumber, InputMethodManager.RESULT_SHOWN);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_left:
			imm.hideSoftInputFromWindow(cardNumber.getWindowToken(), 0);
			finish();
			break;
		case R.id.ok:

			if (!"".equals(cardNumber.getText().toString().trim())) {
				if (!"".equals(card_again.getText().toString().trim())) {
					if (cardNumber.getText().toString().trim().equals(card_again.getText().toString().trim())) {

						if (!"".equals(name.getText().toString().trim())) {

							if (!"".equals(cash_edit.getText().toString().trim())) {

								startProgressDialog();
								bankFare();
							} else {
								Alert.error(this, "请输入转账金额");
							}
						} else {
							Alert.error(this, "请输入姓名");
						}

					} else {
						Alert.error(this, "两次输入账号不一致");
					}
				} else {
					Alert.error(this, "请再次输入转账卡号");
				}

			} else {
				Alert.error(this, "请输入转账卡号");
			}

			break;

		default:
			break;
		}

	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	/**
	 * 银行卡提现
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void bankFare() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "bankFareNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("bankId", bankId));
		params.add(new BasicNameValuePair("bankCode", cardNumber.getText().toString()));
		params.add(new BasicNameValuePair("name", name.getText().toString()));
		params.add(new BasicNameValuePair("count", cash_edit.getText().toString()));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				String result = (String) object;
				Gson gson = new Gson();
				try {
					mCloudBeansBean = gson.fromJson(result, CloudBeansBean.class);
				} catch (Exception e) {
					return;
				}
				if ("0".equals(mCloudBeansBean.getResultCode())) {
					if (!TextUtils.isEmpty(mCloudBeansBean.getCoinAlert())) {
						new PopupWindowAnimation(mainView, CloudCoinsBankRechargeActivity.this);
					}
					stopProgressDialog();
					Alert.info(CloudCoinsBankRechargeActivity.this, "提现成功，请稍后确认");
					setResult(RESULT_OK);
					finish();

				} else {
					stopProgressDialog();
					Alert.error(CloudCoinsBankRechargeActivity.this, mCloudBeansBean.getResultCode());
				}
			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.error(CloudCoinsBankRechargeActivity.this, "与服务器连接失败，请检查网络");

			}
		}, GroupBean.class, AppConfig.USER).execute(params);
	}

}
